﻿using System;
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using Newtonsoft.Json;
using StackExchange.Redis;

namespace NetCoreCMS.Web.Controllers
{
    public class HomeController : Controller
    {
        public IUser_oauth_appService _user_oauth_app { get; set; }
        public IWebHostEnvironment _env { get; set; }
        //public ILogger<HomeController> _logger { get; set; }
        [ResponseCache(Duration = 60)]
        public IActionResult Index()
        {
            //_logger.LogInformation("Hello, this is the index!");
            return Redirect("Web");
        }
        public IActionResult Share(string name)
        {
            var cfg = Common.XMLConfig.Sysconfig.loadConfig(_env.ContentRootPath);
            ViewBag.user_name = name;
            #region wx
            string redisName = Request.Host.Host.ToLower().Replace(".", "_") + "_";
            var app = _user_oauth_app.GetFirst();
            
            string shareUrl =  cfg.weburl + Request.PathBase + Request.Path + Request.QueryString; ;
            var acmodel = WXTokenHelper.GetAccessToken(redisName, app, shareUrl);
            ViewBag.ts = acmodel.ts;
            ViewBag.noncestr = acmodel.noncestr;
            ViewBag.jsapi_ticket = acmodel.jsapi_ticket;
            ViewBag.signature = acmodel.signature;
            ViewBag.shareUrl = shareUrl;
            ViewBag.appid = app.oauth_app_id;
            ViewBag.name = name;
            ViewBag.imgUrl = cfg.weburl+ "/images/share.png";
            ViewBag.company = cfg.webcompany;
            #endregion
            return View();
        }
        public IActionResult Weixin()
        {
            return View();
        }
        public IActionResult Error()
        {
            ViewBag.Msg = Request.Query["msg"];
            return View("/Areas/Web/Views/Error/Index.cshtml");
        }
        public IActionResult Live()
        {
            return View();
        }
    }
}
